﻿Shader "Billboard/BillboardParticlMask" 
    {
        Properties 
        {
            _MainTex ("Base (RGB)", 2D) = "white" {}
			_Mask ("Alpha (A)", 2D) = "white" {}

            _Disappear( "Disappear", Range(0, 20) ) = 8

            _B("Scale time", float) = 0.125
            _C("Scale size", float) = 1
        }

        Subshader 
        {
            Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
			Fog { Mode Off }
			Cull Off
			Blend SrcAlpha  OneMinusSrcAlpha
			ZWrite Off
			Lighting Off
            Pass 
            {
				CGPROGRAM
//				#pragma noambient
                #pragma vertex vert
                #pragma fragment frag
                #pragma fragmentoption ARB_precision_hint_fastest
                #pragma glsl_no_auto_normalization
                #include "UnityCG.cginc"                

                struct v2f 
                { 
                    float4   pos : SV_POSITION;
                    half2   texcoord : TEXCOORD0;
                    fixed4   clr : COLOR;
                };

                half4 _MainTex_ST;
				half4 _Mask_ST;
                float _Disappear;

                float _B;
                float _C;

                v2f vert (appdata_full v)
                {
                    v2f o;
					fixed livetime = v.color.a;
					float time = _Time.y - v.tangent.z;

					float timeRate = time / livetime;

					//xspeed = v.texcoord1.x
					//xacc = v.texcoord1.y
					//yspeed = v.normal.x
					//yacc = v.normal.y
					//movetime = v.normal.z

					float dTime = time - _B;
					dTime *= sqrt(_C) / _B;
					float scale = _C - dTime * dTime;
					scale = max(0.0, scale) * v.tangent.w;
						
					v.tangent.xy += v.tangent.xy * scale;
						
                    float4 camspacePos = v.vertex;
					float canmovetime = min(v.normal.z, time);

					float canMoveFactor = pow(canmovetime, 2);

					camspacePos.x = camspacePos.x + v.texcoord1.x * canmovetime + canMoveFactor  * v.texcoord1.y + v.tangent.x;
                    camspacePos.y = camspacePos.y + v.normal.x * canmovetime + canMoveFactor * v.normal.y + v.tangent.y;
					camspacePos.z = 1;

                    o.pos = mul (UNITY_MATRIX_P, camspacePos) * (1 - min(1, floor(timeRate)));
					o.texcoord = v.texcoord;
                    o.clr = fixed4(1, 1, 1, 1.0 - pow(timeRate, _Disappear));

                    return o;
                }

                sampler2D _MainTex;
				sampler2D _Mask;

                fixed4 frag (v2f i) : COLOR
                {
                    //从_MainTex中根据uv坐标进行采样
					fixed3 _MainTex_Var = tex2D(_MainTex, TRANSFORM_TEX(i.texcoord, _MainTex));
					//从_Mask中根据uv坐标进行采样
					fixed3 _MaskTex_Var = tex2D(_Mask, TRANSFORM_TEX(i.texcoord, _MainTex));
					
					fixed4 renderTex = fixed4(_MainTex_Var,_MaskTex_Var.r * i.clr.a);

					return renderTex;//fixed4(finalColor, renderTex.a);
                }
                ENDCG
            }
        }
		fallback "Mobile/Unlit (Supports Lightmap)"
    }